package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Bom;
import com.synergy.stockflow.product.entity.BomVo;
import com.synergy.stockflow.product.service.BomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bom")
public class BomController {
    @Autowired
    BomService bomService;
    @GetMapping("/index")
    public Result<Page<Bom>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                  @RequestParam(name = "size", defaultValue = "5") int size,
                                  String materialId

    ){
        {
            QueryWrapper<Bom> query =new QueryWrapper<>();
            query.lambda().eq(Bom::getMaterialId,materialId);
            Page<Bom> page = new Page<>(current,size);
            page = bomService.page(page,query);
            return Result.success(page);
        }
    }
    @GetMapping("/boms")
    public Result<List<Bom>> listByMaterialId(String materialId){
        return Result.success(bomService.selectspecificationsAndModels(materialId));
    }


    @PostMapping("insert")
    public Result insert(@RequestBody List<Bom> bom){
        bomService.deleteM(bom.get(0).getMaterialId());
        for(Bom bom1:bom){
            bom1.setCreationTime(new Date());
            bom1.setUpdateTime(new Date());
            bomService.save(bom1);
        }
        return Result.success(bom);
    }


//    @PutMapping("update")
//    public Result update(@RequestBody MaterialDetail materialDetail){
//        System.out.println(materialDetail);
//        materialDetailService.updateById(materialDetail);
//        return Result.success(materialDetail);
//    }
//    @DeleteMapping("/delete")
//    public Result delete(@RequestBody List<MaterialDetail> materialDetail){
//
//        for (MaterialDetail detail:materialDetail){
//        }
//
//        for(MaterialDetail detail:materialDetail){
//            materialDetailService.removeById(detail.getDetailId());
//        }
//        return Result.success(materialDetail);
//    }

    @GetMapping("/selectBom")
    public Result<List<BomVo>> selectBom(String materialId){
        return Result.success(bomService.selectBom(materialId));
    }
}
